package com.prp.gwt.client.widgets.user.friend;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.Widget;
import com.prp.gwt.client.user.friend.FriendListener;

public class FriendView extends Composite {

	interface Binder extends UiBinder<Widget, FriendView> {
	}

	private static final Binder binder = GWT.create(Binder.class);
	private static FriendView instance;

	@UiField
	TextBox searchBox;
	@UiField
	HTML searchImage;
	@UiField
	FriendListPanel friendList;
	@UiField
	InvitationListPanel invitationList;
	@UiField
	RecommendPanel recommend;
	@UiField
	MyRecommendationList myRecommend;

	public static FriendView getInstance() {
		if (instance == null)
			instance = new FriendView();
		return instance;
	}

	private FriendView() {
		initWidget(binder.createAndBindUi(this));
		addStyleName("topPanel");
		searchImage.setHTML("<a><img src=\"" + GWT.getModuleBaseURL()
				+ "icon_search.png\"/>" + "<a>");
	}

	@UiHandler("searchBox")
	public void onSearchBoxKeyUp(KeyUpEvent event) {
		if (event.getNativeKeyCode() == 13)
			search();
	}

	@UiHandler("searchImage")
	public void onSearchImageClick(ClickEvent event) {
		search();
	}

	public void search() {
		String userName = searchBox.getText();
		if (userName != null && !userName.isEmpty()){
			FriendListPanel.getInstance().init();
			FriendListPanel.getInstance().myFriends.setVisible(true);
			FriendListener.getInstance().searchUser(userName);
		}
	}

}
